package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.exception.PauseException;
import com.fdb.efp.nls.service.dao.PspDataUploadDao;
import com.fdb.efp.nls.service.domain.PspDataUpload;
import com.fdb.efp.nls.service.facade.PspDataUploadService;
import com.fdb.efp.nls.service.vo.PspDataUploadVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 贷后凭证上传表				<p>
 * @author chenqm1
 * @date 2018-05-14
 */
@Service("pspDataUploadService")
public class PspDataUploadServiceImpl implements PspDataUploadService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(PspDataUploadServiceImpl.class);

	@Autowired
	private PspDataUploadDao pspDataUploadDao;
	
	/**
	 * 新增操作
	 */
	public int insertPspDataUpload(PspDataUploadVO inPspDataUploadVo) {
		logger.debug("当前新增数据为:"+ inPspDataUploadVo.toString());
		int num = 0;
		try {
			PspDataUpload pspDataUpload = new PspDataUpload();
			beanCopy(inPspDataUploadVo, pspDataUpload);
			num = pspDataUploadDao.insertPspDataUpload(pspDataUpload);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(PspDataUploadVO inPspDataUploadVo) {
		logger.debug("当前删除数据条件为:"+ inPspDataUploadVo);
		int num = 0;
		try {
			PspDataUpload pspDataUpload = new PspDataUpload();
			beanCopy(inPspDataUploadVo, pspDataUpload);
			num = pspDataUploadDao.deleteByPk(pspDataUpload);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPspDataUploadVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(PspDataUploadVO inPspDataUploadVo) {
		logger.debug("当前修改数据为:"+ inPspDataUploadVo.toString());
		int num = 0;
		try {
			PspDataUpload pspDataUpload = new PspDataUpload();
			beanCopy(inPspDataUploadVo, pspDataUpload);
			num = pspDataUploadDao.updateByPk(pspDataUpload);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPspDataUploadVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public PspDataUploadVO queryByPk(PspDataUploadVO inPspDataUploadVo) {
		
		logger.debug("当前查询参数信息为:"+ inPspDataUploadVo);
		try {
			PspDataUpload queryPspDataUpload = new PspDataUpload();
			beanCopy(inPspDataUploadVo, queryPspDataUpload);
			PspDataUpload queryRslPspDataUpload = pspDataUploadDao.queryByPk(queryPspDataUpload);
			if (Objects.nonNull(queryRslPspDataUpload)) {
				PspDataUploadVO outPspDataUploadVo = beanCopy(queryRslPspDataUpload, new PspDataUploadVO());
				logger.debug("当前查询结果为:"+ outPspDataUploadVo.toString());
				return outPspDataUploadVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PspDataUploadVO> queryAllOwner(PspDataUploadVO pspDataUploadVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<PspDataUploadVO> list = null;
		try {
			List<PspDataUpload> pspDataUploads = pspDataUploadDao.queryAllOwnerByPage(pspDataUploadVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ pspDataUploads.size());
			pageSet(pspDataUploads, pspDataUploadVo);
			list = (List<PspDataUploadVO>) beansCopy(pspDataUploads, PspDataUploadVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PspDataUploadVO> queryAllCurrOrg(PspDataUploadVO pspDataUploadVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<PspDataUpload> pspDataUploads = pspDataUploadDao.queryAllCurrOrgByPage(pspDataUploadVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+pspDataUploads.size());
		List<PspDataUploadVO> list = null;
		try {
			pageSet(pspDataUploads, pspDataUploadVo);
			list = (List<PspDataUploadVO>) beansCopy(pspDataUploads, PspDataUploadVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PspDataUploadVO> queryAllCurrDownOrg(PspDataUploadVO pspDataUploadVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<PspDataUpload> pspDataUploads = pspDataUploadDao.queryAllCurrDownOrgByPage(pspDataUploadVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ pspDataUploads.size());
		List<PspDataUploadVO> list = null;
		try {
			pageSet(pspDataUploads, pspDataUploadVo);
			list = (List<PspDataUploadVO>) beansCopy(pspDataUploads, PspDataUploadVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
	
	/**
	 * 审核通过更新状态
	 */
	public int updateStatus(PspDataUploadVO pspDataUploadVO) throws PauseException{
		int num = 0;
		try {
			pspDataUploadVO.setAppStatus(BaseConstant.APPR_STATUS_997);
			PspDataUpload pspDataUpload = new PspDataUpload();
			beanCopy(pspDataUploadVO,pspDataUpload);
			num = pspDataUploadDao.updateByPk(pspDataUpload);
		}catch(Exception e) {
			logger.error("修改数据发生异常!", e);
			throw new PauseException("凭证上传审核更新状态出错!");
		}
		return num;
	}
}
